<template>
	<div style="width: 100%;height: 100%">
		<el-row style="height: 10%;">
			<el-form class="search-form form-inline" label-width="120px" size="mini" ref="searchForm">
				<el-col :span="8">
					<el-form-item class="form-group" label="出发日期：">
						<el-date-picker v-model="searchForm.startTime" type="date" placeholder="选择日期">
						</el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item class="form-group" label="始发地市：">
						<el-input v-model="searchForm.startCity" placeholder="" clearable />
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item class="form-group" label="终点地市：">
						<el-input v-model="searchForm.endCity" placeholder="" clearable />
					</el-form-item>
				</el-col>
			</el-form>
		</el-row>
		<el-row style="height: 10%;">
			<el-button type="primary" size="mini" plain class="el-icon-search" @click="doSearch(1)">查询</el-button>
			<el-button v-if="auth == 0" type="primary" size="mini" plain class="el-icon-edit" @click="doAdd()">新增
			</el-button>
			<el-button v-if="auth == 1" type="primary" size="mini" plain class="el-icon-finished" @click="doTicket()">购票
			</el-button>
			<el-button v-if="auth == 0" type="primary" size="mini" plain class="el-icon-edit" @click="doUpdate()">修改
			</el-button>
			<el-button v-if="auth == 0" type="primary" size="mini" plain class="el-icon-circle-close"
				@click="doDelate()">删除</el-button>
		</el-row>
		<el-row style="height: 75%;">
			<el-table ref="table" :data="tableData" style="position: absolute" height="100%" size="mini"
				highlight-current-row border @current-change="handleCurrentRow"
				@selection-change="handleSelectionChange">
				<el-table-column type="selection" width="60" align="center" />
				<el-table-column prop="routeNo" align="center" label="航线号" :min-width="150" />
				<el-table-column prop="flightNo" align="center" label="航班号" width="200" />
				<el-table-column prop="model" align="center" label="客机型号" width="130" />
				<el-table-column prop="startCity" align="center" label="始发城市" width="80" />
				<el-table-column prop="endCity" align="center" label="终点城市" width="80" />
				<el-table-column prop="startTime" align="center" label="始发时间" width="140" />
				<el-table-column prop="endTime" align="center" label="到达时间" width="140" />
				<el-table-column prop="price" align="center" label="票价(元)" width="80" />
				<el-table-column prop="ticket" align="center" label="余票(张)" width="80" />
			</el-table>
		</el-row>
		<el-row style="height: 5%;">
			<el-pagination :page-sizes="[30, 50, 100]" :current-page="searchForm.pageNo"
				:page-size="searchForm.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount"
				@size-change="handleSizeChange" @current-change="handleCurrentChange" />
		</el-row>

		<el-dialog :title="title" :visible.sync="dialogVisible" width="60%" :before-close="handleClose">
			<el-form class="search-form form-inline" label-width="120px" size="mini" ref="flightFrom">
				<el-row>
					<el-col :span="12">
						<el-form-item class="form-group" label="航班：">
							<el-input v-model="flightFrom.flightNo" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="form-group" label="客机型号：">
							<el-input v-model="flightFrom.model" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item class="form-group" label="始发时间：">
							<el-date-picker v-model="flightFrom.startTime" type="datetime" placeholder="选择日期时间">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="form-group" label="到达时间：">
							<el-date-picker v-model="flightFrom.endTime" type="datetime" placeholder="选择日期时间">
							</el-date-picker>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item class="form-group" label="票价：">
							<el-input v-model="flightFrom.price" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="form-group" label="余票：">
							<el-input v-model="flightFrom.ticket" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item class="form-group" label="选择航线：">
							<el-select v-model="flightFrom.route" clearable placeholder="请选择">
								<el-option v-for="item in routeEnum" :key="item.value" :label="item.label"
									:value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="save">确 定</el-button>
			</span>
		</el-dialog>
		<el-dialog title="购票" :visible.sync="dialogTicketVisible" width="60%" :before-close="handleTicketClose">
			<el-form class="search-form form-inline" label-width="120px" size="mini" ref="orderFrom">
				<el-row>
					<el-col :span="12">
						<el-form-item class="form-group" label="姓名：">
							<el-input v-model="orderFrom.username" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="form-group" label="联系方式：">
							<el-input v-model="orderFrom.phone" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12">
						<el-form-item class="form-group" label="身份证号码：">
							<el-input v-model="orderFrom.cardId" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="form-group" label="航班号：">
							<el-input disabled v-model="orderFrom.flightNo" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogTicketVisible = false">取 消</el-button>
				<el-button type="primary" @click="waitPay">稍后付款</el-button>
				<el-button type="primary" @click="toPay">立即付款</el-button>
			</span>
		</el-dialog>

		<el-dialog title="立即付款" :visible.sync="dialogTicketPayVisible" width="60%" :before-close="handleTicketPayClose">
			<el-form class="search-form form-inline" label-width="120px" size="mini" style="height: 300px;">
				<el-row style="height: 100%;">
					<el-col :span="12" style="height: 100%;">
						<div class="wx"></div>
					</el-col>
					<el-col :span="12" style="height: 100%;">
						<div class="ali"></div>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogTicketVisible = false">取 消</el-button>
				<el-button type="primary" @click="toPayOK">确 定</el-button>
			</span>
		</el-dialog>

	</div>
</template>

<script>
	import axios from 'axios'
	export default {
		props: {
			auth: {
				type: String
			}
		},
		data() {
			return {
				searchForm: {
					startCity: '',
					endCity: '',
					startTime: '',
					pageNo: 1,
					pageSize: 30
				},
				title: '',
				tableData: [],
				totalCount: 0,
				dialogVisible: false,
				selection: [],
				routeEnum: [],
				flightFrom: {
					id: ''
				},
				orderFrom: {

				},
				dialogTicketVisible: false,
				dialogTicketPayVisible: false
			}
		},
		methods: {
			doSearch(pageNo) {
				this.searchForm.pageNo = pageNo
				if (this.searchForm.startTime != '' && this.searchForm.startTime != null) {
					this.searchForm.startTime = this.$dayjs(this.searchForm.startTime).format('YYYY-MM-DD HH:mm:ss')
				}
				this.$axios.post("http://localhost:8888/flight/getPage?pageNum=" + this.searchForm.pageNo +
						"&pageSize=" + this.searchForm.pageSize, this.searchForm)
					.then((data) => {
						console.log(data)
						this.tableData = data.data.records
						this.totalCount = data.data.total
					})
			},
			handleCurrentChange(val) {
				this.doSearch(val)
			},
			handleSizeChange(val) {
				this.searchForm.pageSize = val
				this.doSearch(1)
			},
			handleSelectionChange(selection) {
				this.selection = selection
			},
			doAdd() {
				this.title = '新增航班信息'
				this.dialogVisible = true
			},
			doUpdate() {
				this.title = '修改航班信息'
				console.log(this.selection)
				if (this.selection.length == 1) {
					this.flightFrom = this.selection[0]
					// this.flightFrom.route = this.selection[0].routeNo + "  " + this.selection[0].startCity + '->' + this
					// 	.selection[0].endCity
					this.dialogVisible = true
				} else if (this.selection.length == 0) {
					this.$message.warning('请选择一条信息进行修改');
				} else {
					this.$message.warning('只能选择一条信息进行修改');
				}
			},
			save() {
				// if (typeof this.flightFrom.route === 'string') {
				// 	this.routeEnum.forEach((item) => {
				// 		if (item.label == this.flightFrom.route) {
				// 			this.flightFrom.route = item.value
				// 		}
				// 	})
				// }
				// console.log(typeof this.flightFrom.route)
				// return
				this.flightFrom.routeNo = this.flightFrom.route[0]
				this.flightFrom.startCity = this.flightFrom.route[1]
				this.flightFrom.endCity = this.flightFrom.route[2]
				this.flightFrom.startTime = (this.$dayjs(this.flightFrom.startTime).format('YYYY-MM-DD HH:mm:ss'))
				this.flightFrom.endTime = (this.$dayjs(this.flightFrom.endTime).format('YYYY-MM-DD HH:mm:ss'))
				if (this.flightFrom.id == '') {
					this.$axios.post("http://localhost:8888/flight/insert", this.flightFrom).then((data) => {
						if (data.data == 1) {
							this.dialogVisible = false
							this.flightFrom = {
								id: ''
							}
							this.$message.success('新增成功');
							this.doSearch(1)
						}
					})
				} else {
					this.$axios.post("http://localhost:8888/flight/update", this.flightFrom).then((data) => {
						if (data.data == 1) {
							this.dialogVisible = false
							this.flightFrom = {
								id: ''
							}
							this.$message.success('修改成功');
							this.doSearch(1)
						}
					})
				}
			},
			doDelate() {
				if (this.selection.length == 0) {
					this.$message.warning('请选择一条信息进行修改');
				} else {
					let ids = []
					this.selection.forEach((item) => {
						ids.push(item.id)
					})
					this.$axios.post("http://localhost:9999/student/info/delete?ids=" + ids.join(",")).then((data) => {
						if (data.data >= 1) {
							this.$message.success('删除成功');
							this.doSearch(1)
						}
					})
				}

			},
			initData() {
				return this.$axios.post("http://localhost:8888/route/getList", {})
			},
			doTicket() {
				if (this.selection.length == 1) {
					if (this.selection[0].ticket <= 0) {
						this.$message.warning('暂无余票，请选择其他航班！');
					} else {
						this.orderFrom = this.selection[0]
						this.dialogTicketVisible = true
					}
				} else if (this.selection.length == 0) {
					this.$message.warning('请选择航班进行购票');
				} else {
					this.$message.warning('只能选择一趟航班进行购票');
				}
			},
			toPay() {
				this.dialogTicketPayVisible = true
			},
			waitPay() {
				this.orderFrom.status = '未支付'
				this.$axios.post("http://localhost:8888/order/insert", this.orderFrom).then((data) => {
					this.dialogTicketVisible = false
					this.orderFrom = {}
					this.$message.success('该航班已添加到你的订单，请及时支付！');
				})
			},
			toPayOK() {
				this.orderFrom.status = '已支付'
				this.$axios.post("http://localhost:8888/order/insert", this.orderFrom).then((data) => {
					this.dialogTicketPayVisible = false
					this.dialogTicketVisible = false
					this.orderFrom = {}
					this.$message.success('购票成功！');
				})
			}
		},
		async mounted() {
			console.log(this.$dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'))
			let response = await this.initData()
			response.data.forEach((item) => {
				item.label = item.routeNo + "  " + item.startCity + '->' + item.endCity
				item.value = [item.routeNo, item.startCity, item.endCity]
			})
			this.routeEnum = response.data
			this.doSearch(1)
		}
	}
</script>

<style scoped>
	.el-date-editor.el-input,
	.el-date-editor.el-input__inner {
		width: 100%;
	}

	.el-select {
		display: flex;
	}

	.wx {
		width: 100%;
		height: 100%;
		background: url(../img/wx.png) center no-repeat;
	}

	.ali {
		width: 100%;
		height: 100%;
		background: url(../img/ali.png) center no-repeat;
	}
</style>
